package com.lsm;

import java.util.Calendar;
import java.util.Date;

public class DateTest1 {

    public static void main(String[] args) {
        System.out.println(getXiaoHsTime("昨天 17:46"));
      //  System.out.println(getTime());
    }


    public static int getTime(String shijian) {

        int i = 0;
        Calendar cal = Calendar.getInstance();
        if (shijian.contains("分钟")) {
            i = Integer.parseInt(shijian.replace("分钟前", ""));
            cal.add(Calendar.MINUTE, -i);
        } else if (shijian.contains("小时")) {
            i = Integer.parseInt(shijian.replace("小时前", ""));
            cal.add(Calendar.HOUR_OF_DAY, -i);
        } else if (shijian.contains("天")) {
            i = Integer.parseInt(shijian.replace("天前", ""));
            cal.add(Calendar.DATE, -i);
        } else if (shijian.contains("周")) {
            i = Integer.parseInt(shijian.replace("周前", ""));
            cal.add(Calendar.WEEK_OF_YEAR, -i);
        } else {
            return 0;
        }

        Calendar cal1 = Calendar.getInstance();
        long diffInMillis = cal1.getTimeInMillis() - cal.getTimeInMillis();

        // 将毫秒转换为秒
        int diffInSeconds = (int) diffInMillis / 1000;

        return diffInSeconds;
    }

    /**
     * @return
     * @description: 一秒钟前
     * @author: Jeff
     * @date: 2019年12月20日
     */
    private static Date getTime1() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, -1);
        return cal.getTime();
    }

    /**
     * @return
     * @description: 一分钟前
     * @author: Jeff
     * @date: 2019年12月20日
     */
    private static Date getTime2() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE, -1);
        return cal.getTime();
    }

    /**
     * @return
     * @description: 一小时前
     * @author: Jeff
     * @date: 2019年12月20日
     */
    private static Date getTime3() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR_OF_DAY, -1);
        return cal.getTime();
    }

    /**
     * @return
     * @description: 一天前
     * @author: Jeff
     * @date: 2019年12月20日
     */
    private static Date getTime4() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        return cal.getTime();
    }

    /**
     * @return
     * @description: 一周前
     * @author: Jeff
     * @date: 2019年12月20日
     */
    private static Date getTime5() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_YEAR, -1);
        return cal.getTime();
    }

    /**
     * @return
     * @description: 一月前
     * @author: Jeff
     * @date: 2019年12月20日
     */
    private static Date getTime6() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -1);
        return cal.getTime();
    }

    /**
     * @return
     * @description: 一年前
     * @author: Jeff
     * @date: 2019年12月20日
     */
    private static Date getTime7() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, -1);
        return cal.getTime();
    }


    //25分钟前  1小时前  昨天    1天前
    public static int getXiaoHsTime(String shijian) {

        int i = 0;
        Calendar cal = Calendar.getInstance();
        if (shijian.contains("分钟")) {
            i = Integer.parseInt(shijian.replace("分钟前", ""));
            cal.add(Calendar.MINUTE, -i);
        } else if (shijian.contains("小时")) {
            i = Integer.parseInt(shijian.replace("小时前", ""));
            cal.add(Calendar.HOUR_OF_DAY, -i);
        }  else if (shijian.contains("昨天")) {
            String[] zuo = shijian.replace("昨天 ", "").split(":");
            // 将昨天的日期设置为当前日期的前一天
            cal.add(Calendar.DAY_OF_MONTH, -1);
            // 设置昨天的指定时间（例如12:00）
            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(zuo[0]));
            cal.set(Calendar.MINUTE, Integer.parseInt(zuo[1]));
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
        } else if (shijian.contains("天")) {
            i = Integer.parseInt(shijian.replace("天前", ""));
            cal.add(Calendar.DATE, -i);
        } else {
            return 0;
        }

        Calendar cal1 = Calendar.getInstance();
        long diffInMillis = cal1.getTimeInMillis() - cal.getTimeInMillis();

        // 将毫秒转换为秒
        int diffInSeconds = (int) diffInMillis / 1000;

        return diffInSeconds;
    }


    // 55分钟前   23小时前     1天前   1周前     1月前
    public static int getKuaiSTime(String shijian) {

        int i = 0;
        Calendar cal = Calendar.getInstance();
        if (shijian.contains("分钟")) {
            i = Integer.parseInt(shijian.replace("分钟前", ""));
            cal.add(Calendar.MINUTE, -i);
        } else if (shijian.contains("小时")) {
            i = Integer.parseInt(shijian.replace("小时前", ""));
            cal.add(Calendar.HOUR_OF_DAY, -i);
        } else if (shijian.contains("天")) {
            i = Integer.parseInt(shijian.replace("天前", ""));
            cal.add(Calendar.DATE, -i);
        } else {
            return 0;
        }

        Calendar cal1 = Calendar.getInstance();
        long diffInMillis = cal1.getTimeInMillis() - cal.getTimeInMillis();

        // 将毫秒转换为秒
        int diffInSeconds = (int) diffInMillis / 1000;

        return diffInSeconds;
    }
}

